package org.oos;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

import org.oos.util.Log;

import com.google.api.client.xml.XmlNamespaceDictionary;

public interface Constants {

	public static final String LOG_TAG = "oos4java";

	public static enum Dictionary {
		@EntrySet({
				@Entry(key = "os", value = "http://a9.com/-/spec/opensearch/1.1/"),
				@Entry(key = "oos", value = "http://11870.com/api/oos"),
				@Entry(key = "app", value = "http://www.w3.org/2007/app"),
				@Entry(key = "georss", value = "http://www.georss.org/georss"),
				@Entry(key = "atom", value = "http://www.w3.org/2005/Atom"),
				@Entry(key = "activity", value = "http://activitystrea.ms/spec/1.0/"),
				@Entry(key = "", value = "http://www.w3.org/2005/Atom"),
				@Entry(key = "xml", value = "http://www.w3.org/XML/1998/namespace") })
		Atom, @EntrySet({
				@Entry(key = "os", value = "http://a9.com/-/spec/opensearch/1.1/"),
				@Entry(key = "oos", value = "http://11870.com/api/oos"),
				@Entry(key = "app", value = "http://www.w3.org/2007/app"),
				@Entry(key = "georss", value = "http://www.georss.org/georss"),
				@Entry(key = "atom", value = "http://www.w3.org/2005/Atom"),
				@Entry(key = "activity", value = "http://activitystrea.ms/spec/1.0/"),
				@Entry(key = "xml", value = "http://www.w3.org/XML/1998/namespace") })
		Xml;

		private XmlNamespaceDictionary dictionary = null;

		public XmlNamespaceDictionary getDictionary() {
			XmlNamespaceDictionary result = dictionary;
			if (result == null) {
				synchronized (this) {
					result = dictionary;
					if (result == null) {
						try {
							Field field = getClass().getField(name());
							EntrySet annotation = field
									.getAnnotation(EntrySet.class);
							result = dictionary = initDictionary(annotation);
						} catch (Exception e) {
							Log.e(e);
						}
					}
				}
			}
			return result;
		}

		@Target(FIELD)
		@Retention(RUNTIME)
		@interface EntrySet {
			Entry[] value();
		}

		@Target(FIELD)
		@Retention(RUNTIME)
		@interface Entry {
			String key();

			String value();
		}

		private static XmlNamespaceDictionary initDictionary(
				EntrySet dictionary) {
			XmlNamespaceDictionary xmlDictionary = new XmlNamespaceDictionary();
			for (Entry entry : dictionary.value()) {
				xmlDictionary.set(entry.key(), entry.value());
			}
			return xmlDictionary;
		}

	}

	public static final String COMMA = ",";
	public static final String SPACED_COMMA = ", ";

}
